package com.ydche.service.impl;

import com.ydche.constant.Constant;
import com.ydche.dao.IAccountDao;
import com.ydche.dao.ICustomerDao;
import com.ydche.dao.IUserRoleDao;
import com.ydche.idl_output.*;
import com.ydche.service.IUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ydche.dao.IUserDao;
import com.ydche.util.*;


@Service
public class UserServiceImpl implements IUserService {
    private static Logger log = LogManager.getLogger(UserServiceImpl.class);
    @Autowired
    private IUserDao userDao;
    @Autowired
    private IUserRoleDao userRoleDao;
    @Autowired
    private IAccountDao accountDao;
    @Autowired
    private ICustomerDao customerDao;

    @Override
    public int registerByMobile(UserProfile user) throws RequestException{
        if (userDao.getUserByMobile(user.getMobile()) != null) {
            throw MyException.makeException("该手机号已经存在!");
        }
        int userId = userDao.addUser(initUserProfile(user));
        user.setId(userId);

        userRoleDao.addUserRole(initUserRole(userId));

        accountDao.addAccount(initAccount(userId));

        if (user.getOwner_id() != 0) {
            customerDao.addCustomer(initCustomerInfo(user));
        }

        user.setPwd(MD5.UMencode(user.getPwd(), String.valueOf(user.getId())));
        userDao.updateUser(user);

        return userId;
    }

    @Override
    public UserProfile getUserByMobile(String mobile) throws RequestException {
        return userDao.getUserByMobile(mobile);
    }

    private UserProfile initUserProfile(UserProfile src) {
        src.setIs_enabled(true);
        src.setIs_locked(false);
        src.setPoint(1);
        src.setType_id(Constant.User.UserType.INDIVIDUAL);
        src.setIs_admin(false);
        src.setRegister_time(MyTime.getCurrentDateTime());
        src.setIsRealName(1);
        return src;
    }

    private UserRole initUserRole(int userId) {
        UserRole role = new UserRole();
        role.setRole_id(2);
        role.setUser_id(userId);
        return role;
    }

    private Account initAccount(int userId) {
        Account account = new Account();
        account.setUser_id(userId);
        account.setBalance(MyNumber.scaleDouble(0.0));
        account.setCash(MyNumber.scaleDouble(0.0));
        account.setPay_password("");
        account.setIsLocked(0);
        return account;
    }

    private CustomerInfo initCustomerInfo(UserProfile user) {
        CustomerInfo info = new CustomerInfo();
        info.setUser_id(user.getId());
        info.setCustomer_name(Constant.TBD);
        info.setDoc_code(Constant.TBD);
        info.setDoc_type_id(1);
        info.setIsImmevalid(1);
        info.setIs_locked(true);
        info.setIs_enabled(false);
        info.setOwner_id(user.getOwner_id());
        info.setCreate_date(MyTime.getCurrentDateTime());
        info.setMobile(user.getMobile());
        info.setAlias_name(user.getAlias_name());
        return info;
    }
}
